import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';

enum Api {
  pageDataset = "/infra-service/dataset/page",
  getDataset = "/infra-service/dataset/get",
  saveDataset = "/infra-service/dataset/save",
  updateDataset = "/infra-service/dataset/update",
  deleteDataset = "/infra-service/dataset/delete",
  batchDeleteDataset = "/infra-service/dataset/batchDelete",
  saveGroup = '/infra-service/dataset/saveGroup',
  updateGroup = '/infra-service/dataset/updateGroup',
  batchDeleteGroup = '/infra-service/dataset/batchDeleteGroup',
  deleteGroup = '/infra-service/dataset/deleteGroup',
  importTable = '/infra-service/dataset/importTable',
}

export const importTable = (params) => {
  return defHttp.post({url: Api.importTable, params});
}

export const getDataset = (params) => {
  return defHttp.get({url : Api.getDataset, params});
}

export const pageDataset = (params) => {
  return defHttp.get({url : Api.pageDataset, params});
}

export const saveOrUpdateDataset = (params, isUpdate) => {
  let url = isUpdate ? Api.updateDataset : Api.saveDataset;
  return defHttp.post({url: url, params});
}

export const deleteDataset = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteDataset, data: params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteDataset = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.batchDeleteDataset, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}


export const saveOrUpdateGroup = (params, isUpdate) => {
  let url = isUpdate ? Api.updateGroup : Api.saveGroup;
  return defHttp.post({ url: url, params });
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: {id: params.id} }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}
